#include "LED.h"

static const char *TAG = "GPIO_LED.CPP";



GPIO_LED::GPIO_LED(uint8_t IO_NUM)
{
    IO_Num = IO_NUM;
    gpio_config_t   gpio;               //GPIO结构体

    gpio.pin_bit_mask = (1 << IO_Num);        //GPIO口
    gpio.intr_type = GPIO_INTR_DISABLE;           //中断
    gpio.mode = GPIO_MODE_INPUT_OUTPUT;           //输入/输出模式
    gpio.pull_up_en = GPIO_PULLUP_DISABLE;        //上拉
    gpio.pull_down_en = GPIO_PULLDOWN_DISABLE;    //下拉

    gpio_config(&gpio); //设置GPIO配置
    ESP_LOGI(TAG,"\nGPIO_Init OK!\n");
}

void GPIO_LED::LED_On()
{
    gpio_set_level((gpio_num_t)IO_Num, On_Level);
}

void GPIO_LED::LED_Off()
{
    gpio_set_level((gpio_num_t)IO_Num,Off_level);
}

void GPIO_LED::Level_flip()
{
    gpio_set_level((gpio_num_t)IO_Num,!gpio_get_level((gpio_num_t)IO_Num));
}


/*----------------------------------------------呼吸灯----------------------------------------------*/

//渐变得最大占空比
#define LEDC_TEST_DUTY          4000
//渐变时间
#define LEDC_TEST_FADE_TIME     500


void GPIO_LED::LEDC_FadeEN( ledc_mode_t LEDC_SPEED_MODE,
                       ledc_channel_t LEDC_CHANNEL,
                       ledc_timer_t LEDC_TIMER,
                       int FADE_TIME)
{
    _SPEED_MODE = LEDC_SPEED_MODE;
    _CHANNEL = LEDC_CHANNEL;
    _FADE_TIME = FADE_TIME;

    ledc_timer_config_t ledc_timer;

    ledc_timer.speed_mode = LEDC_SPEED_MODE,           // timer mode
    ledc_timer.duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
    ledc_timer.freq_hz = 5000,                      // frequency of PWM signal
    ledc_timer.timer_num = LEDC_TIMER,            // timer index
    ledc_timer.clk_cfg = LEDC_AUTO_CLK,              // Auto select the source clock

    // Set configuration of timer0 for high speed channels
    ledc_timer_config(&ledc_timer);

    ledc_channel.channel    = LEDC_CHANNEL;
    ledc_channel.duty       = 0;
    ledc_channel.gpio_num   = IO_Num;
    ledc_channel.speed_mode = LEDC_SPEED_MODE;
    ledc_channel.hpoint     = 0;
    ledc_channel.timer_sel  = LEDC_TIMER;
    // Set LED Controller with previously prepared configuration
    ledc_channel_config(&ledc_channel);

    // Initialize fade service.
    ledc_fade_func_install(0);
}

void GPIO_LED::LEDC_FadeUp(uint32_t LEDC_DUTY)
{
    ESP_LOGI(TAG,"\nLEDC fade up\n");
    // printf("\nLEDC fade up\n");

    ledc_set_fade_with_time(ledc_channel.speed_mode,
					ledc_channel.channel, 
                    LEDC_DUTY,
					_FADE_TIME);
	ledc_fade_start(ledc_channel.speed_mode,
					ledc_channel.channel,
                    LEDC_FADE_NO_WAIT);
}

void GPIO_LED::LEDC_FadeDown()
{
    ESP_LOGI(TAG,"\nLEDC fade Down\n");
    // printf("\nLEDC fade Down\n");
    ledc_set_fade_with_time(ledc_channel.speed_mode,
					ledc_channel.channel,
                    0, 
                    _FADE_TIME);
	ledc_fade_start(ledc_channel.speed_mode,
					ledc_channel.channel,
                    LEDC_FADE_NO_WAIT);
}

void GPIO_LED::LEDC_Fade()
{
    ESP_LOGI(TAG,"\nLEDC Fading\n");

    LEDC_FadeUp();

    vTaskDelay(1000 / portTICK_PERIOD_MS);

    LEDC_FadeDown();
    
    vTaskDelay(1000 / portTICK_PERIOD_MS);

}





void GPIO_LED::LEDC_FadeStop()
{
    printf("\nLEDC fade Stop\n");
    // ledc_fade_func_uninstall();
    ledc_stop(_SPEED_MODE,_CHANNEL, 0);
}